<?php

namespace app\controller;

use app\BaseController;
use app\validate\MemberValidate;
use think\exception\ValidateException;
use app\modelservice\MemberService;

class Member extends BaseController
{
    public $memberService;

    public function initialize()
    {
        parent::initialize();  
        $this->memberService = new MemberService;
    }

    public function pageList()
    {
        $page = input('page/d', ONE);
        $pageSize = input('limit/d', DEFAULT_PAGE_SIZE);

        $searchName = input('search_name', '', 'trim');
        $gender = input('gender');
        $mobile = input('mobile');

        $condition = [];
        // 昵称
        if($searchName) {
            $condition[] = ['nickname', 'like', "%{$searchName}%"];
        }
        // 性别
        if($gender) {
            $condition[] = ['gender', '=', $gender];
        }
        //手机号
        if($mobile) {
            $condition[] = ['mobile', '=', $mobile];
        }

        $total = $this->memberService->count($condition);
        $listData = $this->memberService->pageList($condition, $page, $pageSize);
        return pageSuccess($total, $listData);
    }

    // public function options()
    // {

    //     $options = [
    //         'sex_option' => getConstOption('sex_option'),
    //         'user_status_option' => getConstOption('user_status_option')
    //     ];
    //     return success($options);
    // }

    /**
     * 新增
     */
    public function save()
    {
        $params = getRequestData([
            "nickname","mobile","email","password",
            "gender","age","avatar","phrase"
        ]);
        $params = $this->processRequestData($params);

        // validate
        try {
            checkRequest('post');
            validate(MemberValidate::class)->scene('add')->check($params);
        } catch (ValidateException $e){
            return error($e->getError());
        }

        // create
        try {
            $this->memberService->create($params);
            return success(null, 'created');
        } catch (\Throwable $th) {
            return error($th->getMessage());            
        }
    }

    public function update($id)
    {
        $params = getRequestData([
            "nickname","mobile","email","password",
            "gender","age","avatar","phrase"
        ]);
        $params = $this->processRequestData($params);

        // validate
        try {
            checkRequest('put');
            validate(MemberValidate::class)->scene('edit')->check($params);
        } catch (ValidateException $e){
            return error($e->getError());
        }

        // create
        try {
            $this->memberService->update($id, $params);
            return success(null, 'saved');
        } catch (\Throwable $th) {
            return error($th->getMessage());            
        }
    }

    public function batchSave()
    {
        
    }
    public function batchUpdate()
    {
        
    }
    public function export()
    {
        
    }
    public function import()
    {
        
    }

    public function delete()
    {
        $id = input('get.id/d');
        $ids = input('post.ids');

        try {
            checkRequest('delete');
            $this->modelMember->drop($ids);      
            return success(null, '删除成功!');
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }
        
    }

    /**
     * 更新头像
     */
    public function updateAvatar($id)
    {
        // 处理文件上传
        $avatar_file_path = '';

        try {
            checkRequest('put');
            $this->memberService->update($id, ['avatar'=>$avatar_file_path]);
            return success(['avatar'=>$avatar_file_path], 'success!');
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }
    }

    private function processRequestData($requestData)
    {
        return $requestData;
    }
} 